/****************************************************************************
Copyright (c) 2015 Chukong Technologies Inc.
 
http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package iuyfgvbcd;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Looper;

import java.io.IOException;

public class GoogleAdvertisingIdClient {
    private static final String GOOGLE_PACKAGE_NAME = "com.google.android.gms";
    private static final String GOOGLE_ACTION_NAME = "com.google.android.gms.ads.identifier.service.START";

    /**
     * 不可继承，不允许修改
     */
    public static final class AdInfo {
        //只允许赋值一次,赋值后不再改变
        private final String adId;
        private final boolean adTrackingEnable;
        //声明变量不赋初值,构造函数中对变量赋初值。
        public AdInfo(String adId, boolean adTrackingEnable) {
            this.adId = adId;
            this.adTrackingEnable = adTrackingEnable;
        }
        public String getAdId() {
            return adId;
        }
        public boolean isAdTrackingEnable() {
            return adTrackingEnable;
        }
    }
    public static AdInfo getAdInfo(Context context) throws Exception {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            throw new IllegalAccessException("cannot be called from the main thread");
        }
        PackageManager packageManager = context.getPackageManager();
        packageManager.getPackageInfo("com.android.vending", 0);

        AdvertisingConnection connection = new AdvertisingConnection();
        Intent intent = new Intent(GOOGLE_ACTION_NAME);
        intent.setPackage(GOOGLE_PACKAGE_NAME);
        if (context.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
            try {
                AdvertisingInterfaceImpl adInterface = new AdvertisingInterfaceImpl(connection.getBinder());
                AdInfo adInfo = new AdInfo(adInterface.getId(), adInterface.isLimitAdTrackingEnabled(true));
                return adInfo;
            } catch (Exception exception) {
                throw exception;
            } finally {
                context.unbindService(connection);
            }
        }
        throw new IOException("Google Play connection failed");
    }


}
